﻿using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain.DTO;
using B.MES.Customer.Domain;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using B.MES.Customer.Application.Application;
using MiniExcelLibs;

namespace B.MES.Customer.Read.WebApi.Controllers
{
    /// <summary>
    /// 入库管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class StorageController : ControllerBase
    {
        private readonly IStorageApplication _storageApplication;

        public StorageController(IStorageApplication storageApplication)
        {
            _storageApplication = storageApplication;
        }

        /// <summary>
        /// 入库添加
        /// </summary>
        /// <param name="stockIn"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddStockIn(StockInDTO stockIn)
        {
            return Ok(_storageApplication.AddStockIn(stockIn));
        }

        /// <summary>
        /// 显示全部仓库
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetMemusAll()
        {
            PageApiResut<WarehouseMemuItem> pageApiResut = new PageApiResut<WarehouseMemuItem>();
            pageApiResut.Data = _storageApplication.GetMemus();
            return Ok(pageApiResut);
        }

        /// <summary>
        /// 入库显示+分页+查询
        /// </summary>
        /// <param name="productionOrderID"></param>
        /// <param name="stockInSubject"></param>
        /// <param name="stockInDate"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStockInPage(string? productionOrderID, string? stockInSubject, int page, int size)
        {
            try
            {
                return Ok(_storageApplication.GetStockInPage(productionOrderID, stockInSubject, page, size));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除入库信息
        /// </summary>
        /// <param name="stockInId"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteData(int stockInId)
        {
            try
            {
                return Ok(_storageApplication.DeleteData(stockInId));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 入库反填
        /// </summary>
        /// <param name="stockInId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStockInSel(int stockInId)
        {
            try
            {
                return Ok(_storageApplication.GetStockInSel(stockInId));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 入库修改
        /// </summary>
        /// <param name="stockInSel"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateStockInSel(StockInSelDTO stockInSel)
        {
            try
            {
                return Ok(_storageApplication.UpdateStockInSel(stockInSel)) ;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// Excel导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportToExcel()
        {
            try
            {
                var list = _storageApplication.GetAll();
                var stream = new MemoryStream();
                stream.SaveAs(list);
                stream.Seek(0, SeekOrigin.Begin);
                return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                {
                    FileDownloadName = "入库列表"
                };
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
